package com.spring_book.spring_book.controller;


import com.spring_book.spring_book.constant.Constants;
import com.spring_book.spring_book.enums.BookStatusEnum;
import com.spring_book.spring_book.model.BookInfo;
import com.spring_book.spring_book.model.PageRequest;
import com.spring_book.spring_book.model.PageResponse;
import com.spring_book.spring_book.model.Result;
import com.spring_book.spring_book.service.BookService;
import com.sun.source.tree.LambdaExpressionTree;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书，bookInfo：{}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())||
                !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null||
                bookInfo.getPrice()==null||!StringUtils.hasLength(bookInfo.getPublish())){
            return "参数不合法,请重新检查";
        }
        try{
            Integer result = bookService.insertBook(bookInfo);
            if(result == 1){
                return "";
            }
        }catch (Exception e){
            log.error("数据插入发生异常：e{}",e);
        }
            return "数据插入失败，请联系管理员";
    }
    @RequestMapping("/getListByPage")
    public Result<PageResponse<BookInfo>> getListByPage(PageRequest pageRequest, HttpServletRequest request){
        log.info("获取图书列表：pageRequest:{}",pageRequest);
        HttpSession session = request.getSession(false);
        PageResponse<BookInfo> bookInfoPageResponse = new PageResponse<>();
        try {
            bookInfoPageResponse = bookService.getListByPage(pageRequest);
        }catch (Exception e){
            log.error("获取图书列表失败");
            return Result.fail();
        }

        return Result.success(bookInfoPageResponse);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("获取bookId:{}",bookId);
        try {
            if(bookId!=null && bookId>=0){
                return bookService.queryBookById(bookId);
            }
        }catch (Exception e){
            log.error("参数传递错误！e:",e);
            return  null;
        }
        return null;
    }
    @RequestMapping(value = "/updateBook",produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书bookInfo:{}",bookInfo);
        try{
            Integer result = bookService.updateBook(bookInfo);
            if(result>0){
                return "";
            }
            return "图书更新失败";
        }catch (Exception e){
            log.error("数据修改失败，e:",e);
            return "数据库更新失败";
        }
    }

    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书bookId:{}",bookId);
        try{
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(bookId);
            bookInfo.setStatus(BookStatusEnum.DELETE.getCode());
            Integer result = bookService.updateBook(bookInfo);
            if(result>0){
                return "";
            }
            return "图书删除失败";
        }catch (Exception e){
            log.error("数据删除失败，e:",e);
            return "数据库更新失败";
        }
    }
    @RequestMapping("/batchDeleteBook")
    public Boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书 ids:{}",ids);
        try{
            bookService.batchDelete(ids);
            return true;
        }catch (Exception e){
            log.error("批量删除图书失败 ids:{}",ids);
        }
        return false;
    }
}
